package com.yuexunit.finance.system.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.exception.NotFoundException;
import com.yuexunit.finance.system.entity.SystemParamEntity;
import com.yuexunit.finance.system.entity.lexicon.ParamTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */
@Implements(SystemParamService.class)
public class SystemParamServiceImpl implements SystemParamService {

	@Override
	@Transactional
	public Long createSytemParam(SystemParamEntity systemParamEntity) {
		systemParamEntity.setEnabled(Boolean.TRUE);
		systemParamEntity.setCreateDateTime(DateUtil.now());
		systemParamEntity.setUpdateDateTime(DateUtil.now());

		PersistUtil.insert(systemParamEntity);
		return systemParamEntity.getParamId();
	}

	@Override
	@Transactional
	public void updateSytemParam(SystemParamEntity systemParamEntity) {
		SystemParamEntity entity = inquireSytemParamDetail(systemParamEntity.getParamId());
		if (systemParamEntity.getParamName() != null) {
			entity.setParamName(systemParamEntity.getParamName());
		}
		if (systemParamEntity.getParamType() != null) {
			entity.setParamType(systemParamEntity.getParamType());
		}
		if (systemParamEntity.getParamValue() != null) {
			entity.setParamValue(systemParamEntity.getParamValue());
		}
		if (systemParamEntity.getRemark() != null) {
			entity.setRemark(systemParamEntity.getRemark());
		}
		entity.setUpdateDateTime(DateUtil.now());

		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void disableSytemParam(Long SystemParamId) {
		SystemParamEntity entity = inquireSytemParamDetail(SystemParamId);
		entity.setEnabled(false);
		entity.setUpdateDateTime(DateUtil.now());

		PersistUtil.update(entity);
	}

	@Override
	public SystemParamEntity inquireSytemParamDetail(Long SystemParamId) {
		SystemParamEntity entity = PersistUtil.get(SystemParamEntity.class, SystemParamId);
		if (null == entity) {
			throw new NotFoundException("该系统参数不存在");
		}
		return entity;
	}

	@Override
	public PageList<SystemParamEntity> inquireSystemParamList(String SystemParamName, ParamTypeEnum paramType, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SystemParamEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(SystemParamName)) {
			String value = "%" + SystemParamName + "%";
			criteria.add(Restrictions.like("paramName", value));
		}
		if (paramType != null) {
			criteria.add(Restrictions.eq("paramType", paramType));
		}
		PageList<SystemParamEntity> entityList = PersistUtil.get(criteria, page);
		return entityList;
	}

	@Override
	public String inquireSytemParamDetailByName(String paramName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SystemParamEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("paramName", paramName));
		List<SystemParamEntity> entityList = PersistUtil.get(criteria);
		if (entityList.size() == 0) {
			return "";
		}
		return entityList.get(0).getParamValue();
	}

	@Override
	public void disableNews(Long number) {
		SystemParamEntity entity = inquireSytemParamDetail(number);
		entity.setEnabled(false);
		entity.setUpdateDateTime(DateUtil.now());

		PersistUtil.update(entity);
		
	}

	@Override
	public PageList<SystemParamEntity> ipuireCommonSelectotList(String SystemParamName,  ParamTypeEnum paramType, PageCriteria page) {
			DetachedCriteria criteria = DetachedCriteria.forClass(SystemParamEntity.class);
	criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
	if (StringUtil.isNotBlank(SystemParamName)) {
		String value = "%" + SystemParamName + "%";
		criteria.add(Restrictions.like("paramName", value));
	}
	if (paramType != null) {
		criteria.add(Restrictions.eq("paramType", paramType));
	}
	PageList<SystemParamEntity> entityList = PersistUtil.get(criteria, page);
	return entityList;
	}

}
